using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using BVMB.DataLayer.ManagerObjects;
using BVMB.DataLayer.BusinessObjects;
using BVMB.DataLayer.Base;

namespace BVMB.DataLayer.UnitTests
{
	[TestFixture]
    public partial class PhieuDatVeTests : UnitTestbase
    {
        [SetUp]
        public void SetUp()
        {
            manager = managerFactory.GetPhieuDatVeManager();
            manager.Session.BeginTransaction();
        }

        [TearDown]
        public void TearDown()
        {
            manager.Session.RollbackTransaction();
            manager.Dispose();
        }
        
        protected BVMB.DataLayer.ManagerObjects.IPhieuDatVeManager manager;
		
		protected BVMB.DataLayer.BusinessObjects.PhieuDatVe CreateNewPhieuDatVe()
		{
			BVMB.DataLayer.BusinessObjects.PhieuDatVe entity = new BVMB.DataLayer.BusinessObjects.PhieuDatVe();
			
			
			entity.NgayDat = System.DateTime.Now;
			entity.TinhTrangVeDat = true;
			
			using(BVMB.DataLayer.ManagerObjects.IVeManager veManager = managerFactory.GetVeManager())
			    entity.Ve = veManager.GetAll(1)[0];
			
			return entity;
		}
		protected BVMB.DataLayer.BusinessObjects.PhieuDatVe GetFirstPhieuDatVe()
        {
            IList<BVMB.DataLayer.BusinessObjects.PhieuDatVe> entityList = manager.GetAll(1);
            if (entityList.Count == 0)
                Assert.Fail("All tables must have at least one row for unit tests to succeed.");
            return entityList[0];
        }
		
		[Test]
        public void Create()
        {
            try
            {
				BVMB.DataLayer.BusinessObjects.PhieuDatVe entity = CreateNewPhieuDatVe();
				
                object result = manager.Save(entity);

                Assert.IsNotNull(result);
            }
            catch(Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Read()
        {
            try
            {
                BVMB.DataLayer.BusinessObjects.PhieuDatVe entityA = CreateNewPhieuDatVe();
				manager.Save(entityA);

                BVMB.DataLayer.BusinessObjects.PhieuDatVe entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA, entityB);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
		[Test]
		public void Update()
        {
            try
            {
                BVMB.DataLayer.BusinessObjects.PhieuDatVe entityA = GetFirstPhieuDatVe();
				
				entityA.NgayDat = System.DateTime.Now;
				
				manager.Update(entityA);

                BVMB.DataLayer.BusinessObjects.PhieuDatVe entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA.NgayDat, entityB.NgayDat);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Delete()
        {
            try
            {
                BVMB.DataLayer.BusinessObjects.PhieuDatVe entity = GetFirstPhieuDatVe();
				
                manager.Delete(entity);

                entity = manager.GetById(entity.Id);
                Assert.IsNull(entity);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
	}
}

